import { createApp } from 'vue'
import { createPinia } from 'pinia'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import './assets/css/main.css'
import ElementPlus from 'element-plus'
import App from './App.vue'
import router from './router'
import { getConfigForKey, isSSORwork, isRelease, isInLarkApp } from './utils/config'

//日志等级控制
import log from 'loglevel'

window.console.log = log.debug
window.console.debug = log.debug
window.console.info = log.info
window.console.warn = log.warn
window.console.error = log.error

log.setLevel('debug')
if (import.meta.env.PROD) {
  log.setLevel('info')
}

const urlParams = new URLSearchParams(window.location.search)
const loglevel: any = urlParams.get('crcll')
if (loglevel && ['trace', 'debug', 'info', 'warn', 'error']) {
  log.setLevel(loglevel)
}

const app = createApp(App)
const pinia = createPinia()

import { RunWorkH5 } from '@runwork/runwork-help'
if (isSSORwork) {
  console.log('start init rwork SDK')
  app.use(RunWorkH5, rworkConfig())
}

import VConsole from 'vconsole'
if (isInLarkApp) {
  if (getConfigForKey('env') !== 'prd' || window.location.href.includes('vconsole=1')) {
    const vConsole = new VConsole()
  }
}

app.use(pinia)
app.use(router)
app.use(ElementPlus)
app.mount('#app')

function rworkConfig(): {} {
  const config = {
    env: getConfigForKey('rwork_env'),
    appId: getConfigForKey('rwork_app_id'),
    isLocal: !isRelease,
    devUser: isRelease ? '' : 'cyd',
    isVue3: true,
    http: {
      //请求拦截
      request(config: any) {
        return config
      },
      //响应拦截
      response(response: any) {
        return response
      }
    }
  }
  console.log('rworkConfig', config)
  return config
}

//使用明亮模式
localStorage.setItem('el-theme-appearance', 'light')
